//--------------------------------------------------------------------------
// $Id: AssociationSession.java 12 2006-08-28 05:05:13Z zendragon $
//--------------------------------------------------------------------------
// Copyright 2006 Social Brain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//--------------------------------------------------------------------------

package org.socialbrain.openid.consumer;

import org.apache.commons.codec.binary.Hex;

import java.io.Serializable;

/**
 * @author <a href="http://erning.net/">Zhang Erning</a>
 * @version $Id: AssociationSession.java 12 2006-08-28 05:05:13Z zendragon $
 */
public class AssociationSession implements Serializable
{
    public AssociationSession(String endpoint, String assocHandle, String assocType, int expiresIn, byte[] macKey)
    {
        _endpoint = endpoint;

        _assocHandle = assocHandle;
        _assocType = assocType;
        _expiration = expiresIn * 1000 + System.currentTimeMillis();
        _macKey = macKey;
    }

    @Override
    public boolean equals(Object obj)
    {
        return _endpoint.equals(obj);
    }

    @Override
    public int hashCode()
    {
        return _endpoint.hashCode();
    }

    @Override
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("endpoint:").append(_endpoint).append(';');
        sb.append("assocHandle:").append(_assocHandle).append(';');
        sb.append("assocType:").append(_assocType).append(';');
        sb.append("expiresIn:").append(getExpiresIn()).append(';');
        sb.append("macKey:").append(Hex.encodeHex(_macKey));
        return sb.toString();
    }

    //

    public String getEndpoint()
    {
        return _endpoint;
    }

    public String getAssocHandle()
    {
        return _assocHandle;
    }

    public String getAssocType()
    {
        return _assocType;
    }

    public long getExpiration()
    {
        return _expiration;
    }

    public int getExpiresIn()
    {
        return (int)((_expiration - System.currentTimeMillis()) / 1000);
    }


    public boolean isExpired()
    {
        return System.currentTimeMillis() >= _expiration;
    }

    public byte[] getMacKey()
    {
        return _macKey;
    }

    private String _endpoint;

    private String _assocHandle;
    private String _assocType;
    private long _expiration;

    private byte[] _macKey;
}
